* Placebo shooting regressions

cap mkdir "`out'"

loc shoot_sizes "5.6 10"  // put two fatality counts here
loc miter = 200  // maximum number of iterations for estimation

if "`placebo_dvs'" == "" {
    di "Using default dependent variables for placebo analysis."
    loc placebo_dvs "`billdv' `lawdv' laws_tighten laws_loosen"
}

// how likely are shootings within each state?
capture drop mushoot
egen mushoot = mean(`shoot') if year >= 1989, by(fips)
la var mushoot "Shooting probability conditional on state"

// possible fatality counts
capture drop posfatal
qui gen int posfatal = `fatal' if `fatal' != 0
mkmat posfatal, matrix(F) nomiss
capture count if !mi(posfatal)
loc idx_fatal = r(N)
drop posfatal


// placebo runs {{{
// ----------------------------------------------------------------------------
matrix fake = (., ., ., ., ., ., ., ., .)  // 9
matrix poli = (., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., .) // 20
forvalues i = 1/`placebo_runs' {

    // fake shooting variables (dummy and fatality count) {{{
    capture drop rndshoot
    gen byte rndshoot = runiform() < mushoot & shoot == 0 & year >= 1989
    la var rndshoot "Placebo shooting dummy"

    capture drop rndfatal  // fake fatality count
    gen int rndfatal = floor((`idx_fatal' - 1) * runiform() + 1)
    qui replace rndfatal = F[rndfatal, 1]
    qui replace rndfatal = 0 if rndshoot == 0
    la var rndfatal "Placebo fatality count"  // }}}

    // run regressions on fake shootings and collect coefficients
    matrix P = (., ., ., ., ., ., ., ., .)  // 9
    matrix Y = (., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., .) // 20
    loc rnames = ""
    loc pol_rnames = ""
    foreach dv in `placebo_dvs' {

        // main placebo effects {{{
        qui xtpoisson `dv' L.rndshoot `controls' `rng', `opt' iter(`miter')
        matrix D = (e(converged), _b[L.rndshoot], _se[L.rndshoot], ., ., ., ., ., .)
        matrix P = P \ D

        qui xtpoisson `dv' L.rndfatal `controls' `rng', `opt' iter(`miter')
        matrix D = (e(converged), ., ., _b[L.rndfatal], _se[L.rndfatal])

        // calculate effects for average and big placebo shooting
        foreach x in `shoot_sizes' {
            qui lincom L.rndfatal*`x'
            matrix D = (D, r(estimate), r(se))
        }

        matrix P = P \ D
        loc rnames "`rnames' `dv':dummy `dv':fatal"
        // }}}

        // create interaction between fake shootings and politics {{{
        foreach gv in "demleg" "repleg" "splleg" {
            loc lblp: var label `gv'

            // interaction with fatalities
            capture drop rndfatalX`gv'
            qui gen rndfatalX`gv' = L.rndfatal * `gv'
            la var rndfatalX`gv' "`lblp' X Placebo Fatalities"

            // interaction with shooting dummy
            capture drop rndshootX`gv'
            qui gen rndshootX`gv' = L.rndshoot * `gv'
            la var rndshootX`gv' "`lblp' X Placebo Shooting"
        }  // }}}

        // placebo regressions for political interactions {{{
        foreach p in leg {
            quietly {
                // politics of shooting dummy only
                xtpoisson `dv' L.rndshoot rndshootXrep`p' rndshootXdem`p' /*
                    */ `controls' `rng', `opt' iter(`miter')
                matrix T = (e(converged))

                // republican reponse to placebo dummy
                lincom L.rndshoot + rndshootXrep`p'
                matrix T = (T, r(estimate), r(se))

                // democrat response to placebo dummy
                lincom L.rndshoot + rndshootXdem`p'
                matrix T = (T, r(estimate), r(se))

                // split party response to placebo dummy
                lincom L.rndshoot
                matrix T = (T, r(estimate), r(se))


                // politics and fatalities count {{{
                xtpoisson `dv' L.rndfatal rndfatalXrep`p' rndfatalXdem`p' /*
                    */ `controls' `rng', `opt' iter(`miter')
                matrix T = (T, e(converged))

                foreach x in `shoot_sizes' {
                    // republican reponse to placebo shooting
                    lincom L.rndfatal*`x' + rndfatalXrep`p'*`x'
                    matrix T = (T, r(estimate), r(se))

                    // democrat response to placebo shooting
                    lincom L.rndfatal*`x' + rndfatalXdem`p'*`x'
                    matrix T = (T, r(estimate), r(se))

                    // split party response to placebo shooting
                    lincom L.rndfatal*`x'
                    matrix T = (T, r(estimate), r(se))
                }
                loc pol_rnames = "`pol_rnames' `dv':`p'"
                matrix Y = Y \ T
                // }}}

            }  // end quietly
        }  // end placebo political interaction regressions }}}

    }  // end loop over dependent variables

    // combine coefficient matrices for these placebo regressions {{{
    matrix P = P[2..., 1...]
    matrix rownames P = `rnames'
    matrix fake = fake \ P

    matrix Y = Y[2..., 1...]
    matrix rownames Y = `pol_rnames'
    matrix poli = poli \ Y  // }}}

    if (mod(`i', 10) == 0) di ". ", _continue
}  // end loop over placebo runs }}}


// clean up and label matrices of placebo coefficients {{{
// ----------------------------------------------------------------------------
drop rndshoot* rndfatal*
di "Finished `placebo_runs' placebo shooting analyses"
matrix fake = fake[2..., 1...]
matrix colnames fake = cv shoot se_shoot fatal se_fatal avg se_avg big se_big
matrix poli = poli[2..., 1...]
matrix colnames poli = cv_dum dum_rep se_dum_rep dum_dem se_dum_dem dum_spl /*
    */ se_dum_spl cv_fat avg_rep se_avg_rep avg_dem se_avg_dem avg_spl      /*
    */ se_avg_spl big_rep se_big_rep big_dem se_big_dem big_spl se_big_spl
// }}}


// save matrix of main placebo effects as a dataset {{{
// ----------------------------------------------------------------------------
preserve
    drop _all
    qui svmat2 fake, names(col) rnames(dv) full

    qui split dv, parse(":") limit(2)
    drop dv
    rename (dv1 dv2) (dv mdl)

    // test statistics and labels
    foreach v of varlist shoot fatal avg big {
        qui gen double z`v' = `v' / se_`v'
        la var `v'    "Coefficient for `v'"
        la var se_`v' "Standard error for `v'"
        la var z`v'   "Test statistic for `v'"
    }

    order dv mdl cv
    la var dv "Dependent variable"
    la var mdl "Model type"
    la var cv "Converged"
    la data "Placebo shooting effects, main models"
    qui compress
    capture confirm file "`out'/placebo_main_effects.dta"
    if !_rc {
        qui append using "`out'/placebo_main_effects.dta"
    }
    save "`out'/placebo_main_effects.dta", replace
restore  // }}}


// save matrix of political placebo effects as a dataset {{{
// ----------------------------------------------------------------------------
preserve
    drop _all
    qui svmat2 poli, names(col) rnames(dv) full

    qui split dv, parse(":") limit(2)
    drop dv
    rename (dv1 dv2) (dv pol)

    // test statistics and labels
    foreach v of varlist dum_* avg_* big_* {
        qui gen double z`v' = `v' / se_`v'
        la var `v'    "Coefficient for `v'"
        la var se_`v' "Standard error for `v'"
        la var z`v'   "Test statistic for `v'"
    }

    order dv pol cv_dum cv_fat
    la var dv "Dependent variable"
    la var pol "Political interaction terms"
    la var cv_dum "Model with only dummy converged"
    la var cv_fat "Model with fatality count converged"
    la data "Placebo shooting effects, political models"
    qui compress
    capture confirm file "`out'/placebo_pol_effects.dta"
    if !_rc {
        qui append using "`out'/placebo_pol_effects.dta"
    }
    save "`out'/placebo_pol_effects.dta", replace
restore  // }}}

// vim: foldmethod=marker
